/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.platform.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.medusa.gruul.common.core.constant.CommonConstants;
import com.medusa.gruul.common.core.util.PageUtils;
import com.medusa.gruul.platform.api.entity.AgentNotice;
import com.medusa.gruul.platform.constant.AgentNoticeEnum;
import com.medusa.gruul.platform.mapper.AgentNoticeMapper;
import com.medusa.gruul.platform.model.vo.agent.NoticeInfoVo;
import com.medusa.gruul.platform.model.vo.agent.NoticeInfosVo;
import com.medusa.gruul.platform.service.IAgentNoticeService;
import com.medusa.gruul.platform.stp.StpAgentUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 代理商推送消息表结构 服务实现类
 * </p>
 *
 * @author whh
 * @since 2020-10-04
 */
@Service
@Slf4j
public class AgentNoticeServiceImpl extends ServiceImpl<AgentNoticeMapper, AgentNotice> implements IAgentNoticeService {


    @Override
    public void newNotice(Long agentId, AgentNoticeEnum noticeEnum, LinkedList<String> titleList, Map<String, String> content, Integer msgType) {
        AgentNotice agentNotice = new AgentNotice();
        agentNotice.setAgentInfoId(agentId);
        agentNotice.setIsRead(CommonConstants.NUMBER_ZERO);
        agentNotice.setType(noticeEnum.getType());
        //动态标题，进行标题封装
        if (CollectionUtil.isNotEmpty(titleList)) {
            String title = noticeEnum.getTitle();
            for (String s : titleList) {
                title = StrUtil.format(title, s);
            }
            agentNotice.setTitle(title);
        } else {
            agentNotice.setTitle(noticeEnum.getTitle());
        }
        agentNotice.setContent(JSON.toJSONString(content));
        agentNotice.setMsgType(msgType);
        this.baseMapper.insert(agentNotice);
    }

    @Override
    public PageUtils<NoticeInfosVo> noticeInfos(Integer page, Integer size, String search, Integer type) {
        Page<AgentNotice> resultIpage = this.baseMapper.
                selectPage(new Page<>(page, size), new QueryWrapper<AgentNotice>()
                        .eq("agent_info_id", StpAgentUtil.getLoginId())
                        .like(StrUtil.isNotEmpty(search), "title", search)
                        .eq(type != null && type >= 1, "msg_type", type)
                        .orderByDesc("create_time")
                );
        // 封装返回对象
        PageUtils<NoticeInfosVo> result = new PageUtils<>(null, (int) resultIpage.getTotal(), (int) resultIpage.getSize(), (int) resultIpage.getCurrent());
        // 拷贝部分属性
        if (CollectionUtil.isEmpty(resultIpage.getRecords())) {
            return result;
        }
        List<NoticeInfosVo> infosVos = resultIpage.getRecords().stream().map(obj -> BeanUtil.toBean(obj, NoticeInfosVo.class)).collect(Collectors.toList());
        result.setList(infosVos);
        return result;
    }

    @Override
    public NoticeInfoVo detail(Long id) {
        AgentNotice agentNotice = this.baseMapper.selectById(id);
        if (agentNotice == null) {
            return null;
        }
        if (agentNotice.getIsRead().equals(CommonConstants.NUMBER_ZERO)) {
            agentNotice.setIsRead(CommonConstants.NUMBER_ONE);
            this.baseMapper.updateById(agentNotice);
        }
        return BeanUtil.toBean(agentNotice, NoticeInfoVo.class);
    }
}
